
import urllib
import os
import json
import operator

# this gets a JSON object from a file on the file system
# 
def get_json_object_from_file(file_name):

   f=open(file_name)
   j=json.load(f)
   return j

def get_json_object_from_web():
   
   url='http://www.ebi.ac.uk/pdbe-apps/jsonizer/latest/released/'
   a=urllib.urlopen(url).read()
   j=json.loads(a)
   return j

def journal_info():

   # How shall we get our JSON object?  From the web for a file?
   # 
   # let's get it from the web:
   ja=get_json_object_from_web()
   # ja=get_json_object_from_file('latest-releases.json')
   
   journal_dict={}
   for ent in (ja['ResultSet']['Result']):
      id=ent['EntryID']
      citation=ent['Citation']
      journal=citation[0]['Journal']
      try:
         journal_dict[journal]+=1
      except KeyError:
         journal_dict[journal]=1

   sorted_ls = sorted(journal_dict.items(), key=operator.itemgetter(1))
   
   for journal in sorted_ls:  
      print journal[1], journal[0]

journal_info()
